package com.ruoyi.common.utils;

import com.google.common.base.Joiner;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

/**
 * @author by jianan.liu on 17/4/7.
 */
public class ChangeOrderUtil {

    private static final String CHANGE_PREFIX = "C";
    private static final String CHANGE_MIDDLE = "S";
    private static final Joiner joiner = Joiner.on("").skipNulls();


    public static String buildChangeOrderNo(String orderNo, long servOrder) {
        return joiner.join(CHANGE_PREFIX, orderNo, CHANGE_MIDDLE, servOrder);
    }

    /**
     * 根据改期订单号解析订单号
     *
     * @param changeOrderNo
     * @return
     */
    public static String decodeOrderNo(String changeOrderNo) {
        if (StringUtils.isNotEmpty(changeOrderNo)) {
            String baseNo = "";
            baseNo = changeOrderNo.substring(1);
            String bizOrderNo = baseNo.split("S")[0];
            return bizOrderNo;
        }
        return null;
    }

    /**
     * 根据改期订单号解析服务单号
     *
     * @param changeOrderNo
     * @return
     */
    public static int decodeServOrderId(String changeOrderNo) {
        if (StringUtils.isNotEmpty(changeOrderNo)) {
            String baseNo = "";
            baseNo = changeOrderNo.substring(1);
            int servOrderId = NumberUtils.toInt(baseNo.split("S")[1]);
            return servOrderId;
        }
        return 0;
    }
}
